package org.jeecg.modules.wlhy.customerwallet.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.wlhy.customerwallet.entity.HyCustomerWallet;
import org.jeecg.modules.wlhy.customerwallet.entity.HyCustomerWalletRecharge;
import org.jeecg.modules.wlhy.customerwallet.mapper.HyCustomerOfflineWalletMapper;
import org.jeecg.modules.wlhy.customerwallet.service.IHyCustomerWalletRechargeService;
import org.jeecg.modules.wlhy.customerwallet.service.IHyCustomerWalletService;
import org.jeecg.modules.wlhy.shipping.service.IHyShippingNoteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @Description: hy_customer_offline_wallet
 * @Author: jeecg-boot
 * @Date:   2021-10-20
 * @Version: V1.0
 */
@Service
public class HyCustomerWalletServiceImpl extends ServiceImpl<HyCustomerOfflineWalletMapper, HyCustomerWallet> implements IHyCustomerWalletService {

    @Autowired
    private IHyCustomerWalletRechargeService hyCustomerWalletRechargeService;

    @Autowired
    private IHyShippingNoteService hyShippingNoteService;

    @Override
    public HyCustomerWallet getByCustomerId(String customerId) {
        return this.getOne(new QueryWrapper<HyCustomerWallet>().eq("customer_id",customerId));
    }

    @Override
    public BigDecimal getDJSByCustomerId(String customerId) {
//        return baseMapper.getDJSByCustomerId(customerId);
        return hyShippingNoteService.getCustomerDjsFee(customerId);
    }

    @Override
    public HyCustomerWallet getWalletInfo(String customerId) {
        HyCustomerWallet wallet  = this.getByCustomerId(customerId);
        if(wallet == null) {
            return null;
        }
        //计算待支付运费
        BigDecimal djsAmt = this.getDJSByCustomerId(customerId);
        wallet.setDjsAmt(djsAmt);
        return wallet;
    }

    @Override
    @Transactional
    public HyCustomerWalletRecharge exchange(HyCustomerWallet customerWallet, BigDecimal amt, String channel, String remark) {

        BigDecimal walletAmt = customerWallet.getAmt();
        BigDecimal newAmt = walletAmt.subtract(amt);
        customerWallet.setAmt(newAmt);
        this.updateById(customerWallet);

        //添加扣款记录
        HyCustomerWalletRecharge walletRecharge = new HyCustomerWalletRecharge();
        walletRecharge.setCustomerId(customerWallet.getCustomerId());
        walletRecharge.setOt("2");//支付运费
        walletRecharge.setRt("2");//支出
        walletRecharge.setPayChannel(channel);
        walletRecharge.setWalletId(customerWallet.getId());
        walletRecharge.setAmt(amt);
        walletRecharge.setLastAmt(newAmt);
        walletRecharge.setRemark(remark);
        hyCustomerWalletRechargeService.save(walletRecharge);
        return walletRecharge;
    }

    @Override
    @Transactional
    public void recharge(HyCustomerWallet wallet, BigDecimal amt, String remark) {

        BigDecimal oldAmt = wallet.getAmt();
        BigDecimal newAmt = oldAmt.add(amt);
        wallet.setAmt(newAmt);
        this.updateById(wallet);

        //添加充值记录
        HyCustomerWalletRecharge walletRecharge = new HyCustomerWalletRecharge();
        walletRecharge.setCustomerId(wallet.getCustomerId());
        walletRecharge.setOt("1");//充值
        walletRecharge.setRt("1");//收入
        walletRecharge.setWalletId(wallet.getId());
        walletRecharge.setAmt(amt);
        walletRecharge.setLastAmt(newAmt);
        walletRecharge.setRemark(remark);
        hyCustomerWalletRechargeService.save(walletRecharge);
    }

    @Override
    @Transactional
    public void kc(HyCustomerWallet wallet, BigDecimal amt, String remark) {

        BigDecimal oldAmt = wallet.getAmt();
        //<
        if(oldAmt.compareTo(amt) == -1){
            throw new JeecgBootException("超出钱包余额！");
        }

        BigDecimal newAmt = oldAmt.subtract(amt);
        wallet.setAmt(newAmt);
        this.updateById(wallet);

        //添加扣除记录
        HyCustomerWalletRecharge walletRecharge = new HyCustomerWalletRecharge();
        walletRecharge.setCustomerId(wallet.getCustomerId());
        walletRecharge.setOt("3");//扣除
        walletRecharge.setRt("2");//支出
        walletRecharge.setWalletId(wallet.getId());
        walletRecharge.setAmt(amt);
        walletRecharge.setLastAmt(newAmt);
        walletRecharge.setRemark(remark);
        hyCustomerWalletRechargeService.save(walletRecharge);
    }

    @Override
    public HyCustomerWallet getByCustomerIdForUpdate(String customerId) {
        return baseMapper.getByCustomerIdForUpdate(customerId);
    }
}
